﻿using AutoMapper;
using Common.Property;
using Common.Utility;
using Domain.Sys;
using DTO;
using DTO.DTO.Sys.AdminUser;
using IServices.Sys;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Web.Controllers;

namespace Web.Areas.Sys.Controllers
{
    public class AdminUserController : BaseController
    {
        private readonly IAdminUserService _service;

        public AdminUserController(IHttpContextAccessor httpContextAccessor, IMapper mapper, IAdminUserService service) : base(httpContextAccessor, mapper)
        {
            _service = service;
        }
        // GET: Sys/AdminUser
        public IActionResult Index()
        {
            return View();
        }

        public async Task<ResultList> List(AdminUserQueryDTO dto)
        {
            var result = await _service.GetPageList(dto);
            var map = _mapper.Map<List<AdminUserListDTO>>(result.Item1);
            return ResultList.Success(count: result.Item2, data: map);
        }

        public async Task<Result> Del(string id)
        {
            return await _service.Delete(id, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<Result> Delist(List<string> ids)
        {
            return await _service.DeleteBatch(ids, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<IActionResult> Edit(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                var initModel = new AdminUserDetailDTO();
                return View(initModel);
            }
            else
            {
                var model = await _service.GetById(id);
                model.Password = Const.SecretStr;
                var modelDTO = _mapper.Map<AdminUserDetailDTO>(model);
                return View(modelDTO);
            }
        }

        public async Task<Result> Save(AdminUserDetailDTO model)
        {
            return await _service.Save(model, OperatorInfo);
        }

        public async Task<IActionResult> EditPassword()
        {
            return View();
        }

        public async Task<Result> SavePassword(AdminUserPasswordDTO dto)
        {
            dto.Id = UserInfo.UserId;
            return await _service.SavePassword(dto, OperatorInfo);
        }
    }
}